﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace A36_sql
{
    public partial class WebForm4 : System.Web.UI.Page
    {

        SqlConnection cn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.cn = new SqlConnection();
            this.cn.ConnectionString = "Server=SLAREK-PC\\SLMSSQL2008;Database=natation2;User ID=natation;Password=123456$";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            getNombredeCompet();
        }

        protected void getNombredeCompet() {
            cn.Open();

            this.cmd = new SqlCommand();
            this.cmd.Connection = this.cn;

            this.cmd.CommandText = "spGetNombreCompetition";
            this.cmd.CommandType = CommandType.StoredProcedure;
            this.cmd.Parameters.Add("@Nom", SqlDbType.VarChar).Value = this.TextBox1.Text;
            this.cmd.Parameters.Add("@Prenom", SqlDbType.VarChar).Value = this.TextBox2.Text;
            
            string result = this.cmd.ExecuteScalar().ToString();

            if (result != "-1")
            {
                this.Label1.Text = result;
            }
            else
            {
                this.Label1.Text = "Le user n'existe pas";
            }

            /*
             Create procedure dbo.spGetNombreCompetition
                @Nom varchar(50),
                @Prenom varchar(50)
                as
                if  (Select count(nom) from Nageurs where Nom = @Nom and Prenom = @prenom) = 1
	                begin
		                select NbCompetitions from Nageurs where Nom = @Nom and Prenom = @Prenom
	                end 
                else
	                begin
		                Select -1
	                end
                go
             */
           
            if (cn != null)
            {
                cn.Close();
            }
        }
    }
}